$(function () {

    //变量抽取
    var temOrder_dataGrid = $("#temOrder_dataGrid");
    var temOrder_dialog = $("#temOrder_dialog");
    var temOrder_form = $("#temOrder_form");

    var client_dataGrid = $("#client_dataGrid");
    var client_dialog = $("#client_dialog");
    var car_dataGrid = $("#car_dataGrid");
    var car_dialog = $("#car_dialog");
    var product_dataGrid = $("#product_dataGrid");
    var product_dialog = $("#product_dialog");


    //客户数据表格
    client_dataGrid.datagrid({
        url: "/client/query.do",
        fitColumns: true,//列自适应
        fit: true,
        align: 'center',
        columns: [[
            {
                field: 'clientName', title: '客户姓名', width: 100
            },
            {
                field: 'gender', title: '性别', width: 100, formatter: function (value) {
                return value ? "男" : "女";
            }
            },
            {field: 'idCard', title: '身份证', width: 100, align: 'center'},
            {field: 'tel', title: '联系方式', width: 100, align: 'center'},
            {field: 'clientAddress', title: '客户地址', width: 100, align: 'center'}
        ]],
        pagination: true,//分页
        rownumbers: true,//行号
        singleSelect: true,//只允许选择一行
        //用户点击一行回显数据
        onClickRow: function () {
            var client = client_dataGrid.datagrid("getSelected");

            $("[name='holder.id']").val(client.id);
            $("#clientName").textbox("setValue", client.clientName);
            $("#gender").combobox("setValue", client.gender);
            $("#idCard").textbox("setValue", client.idCard);
            $("#tel").numberbox("setValue", client.tel);
            $("#clientAddress").textbox("setValue", client.clientAddress);
            /*$("#add_form").form("load", client);*/
            client_dialog.dialog("close");
        }
    });

    //车辆数据表格
    car_dataGrid.datagrid({
        fitColumns: true,//列自适应
        url: "/car/query.do",
        fit: true,
        columns: [[
            {field: 'name', title: '车主姓名', width: 100},
            {
                field: 'carbrand', title: '车辆品牌', width: 100, formatter: function (value) {
                var name;
                $.ajax({
                    type: "POST",
                    url: "/dictionary/selectDictionaryItemById.do",
                    async: false,
                    data: {id: value},
                    success: function (msg) {
                        name = msg.name;
                    }
                });
                return name;
            }
            },
            {field: 'model', title: '车辆型号', width: 100},
            {field: 'number', title: '车牌号码', width: 100},
            {field: 'gas', title: '排气量/L', width: 100},
            {field: 'pdate', title: '购买日期', width: 100},
            {field: 'price', title: '市场估价/万元', width: 100, sortable: true},
            {
                field: 'typestate', title: '分类', width: 100, formatter: function (value, row, index) {
                return value ? "<span style='color: mediumblue'>载客</span>" : "<span style='color: orangered'>载物</span>";
            }
            },
            {
                field: 'weight', title: '车载大小', width: 100, formatter: function (value) {
                var name;
                $.ajax({
                    type: "POST",
                    url: "/dictionary/selectDictionaryItemById.do",
                    async: false,
                    data: {id: value},
                    success: function (msg) {
                        name = msg.name;
                    }
                });
                return name;
            }
            }
        ]],
        pagination: true,//分页
        rownumbers: true,//行号
        singleSelect: true,//只允许选择一行
        sortName: 'price',
        sortOrder: 'DESC',
        //用户点击一行回显数据
        onClickRow: function () {
            var car = car_dataGrid.datagrid("getSelected");
            $("#cId").val(car.id);
            $("#cName").textbox("setValue", car.name);
            $("#cBrand").combobox("setValue", car.carbrand);
            $("#cModel").textbox("setValue", car.model);
            $("#cNumber").textbox("setValue", car.number);
            $("#cGas").textbox("setValue", car.gas);
            $("#cDate").datebox("setValue", car.pdate);
            $("#cPrice").numberbox("setValue", car.price);
            $("#cType").combobox("setValue", car.typestate);
            $("#cWeight").combobox("setValue", car.weight);
            /*$("#add_form").form("load", car);*/
            car_dialog.dialog("close");
        }
    });

    //保险信息
    product_dataGrid.datagrid({
        fitColumns: true,//列自适应
        url: "/product/query.do",
        fit: true,
        columns: [[
            {field: 'sn', title: '保险编号', width: 100},
            {field: 'name', title: '保险名称', width: 100},
            {field: 'year', title: '保障年限', width: 100},
            {field: 'money', title: '保额', width: 100},
            {field: 'annualFee', title: '基本年费', width: 100},
            {
                field: 'saleState', title: '销售状态', width: 100, formatter: function (value, row, index) {
                return value ? "在售" : "<font color='red'>停售</font>";
            }
            },
            {
                field: 'deductState', title: '免赔状态', width: 100, formatter: function (value, row, index) {
                return value ? "是" : "<font color='red'>否</font>";
            }
            }
        ]],
        queryParams: {
            saleState: 1,
            companyId: function () {
                return $("#companyId").combobox("getValue");
            }
        },
        pagination: true,//分页
        rownumbers: true,//行号
        onClickRow: function (index, row) {
            $("#productName").textbox("setValue", row.name);
            $("[name='product.id']").val(row.id);
            $("[tag=sn]").text(row.sn);
            $("#annualFee").textbox("setValue", row.annualFee);
            product_dialog.dialog("close");
        }
    });


    //初始化客户弹出框
    client_dialog.dialog({
        width: 480,
        height: 380,
        closed: true
    });

    //初始化车辆弹出框
    car_dialog.dialog({
        width: 800,
        height: 380,
        closed: true
    });

    //初始化保险信息弹出框
    product_dialog.dialog({
        width: 530,
        height: 380,
        closed: true,
    });


    temOrder_dataGrid.datagrid({
        fitColumns: true,//列自适应
        url: "/temOrder/query.do",
        fit: true,
        columns: [[
            {field: 'sn', title: '保单编号', width: 100},
            {
                field: 'holder', title: '投保人', width: 100, formatter: function (value, row, index) {
                return value ? value.clientName : "";
            }
            },
            {
                field: 'carNo', title: '车牌号', width: 100, formatter: function (value, row, index) {
                return value ? value.number : "";
            }
            },
            {
                field: 'adate', title: '申请时间', width: 100, formatter: function (value, row, index) {
                return value ? new Date(value).toLocaleDateString() : '';
            }
            },
            {field: 'pDate', title: '核保日期', width: 100},
            {field: 'ddate', title: '保险截止日', width: 100},
            {field: 'year', title: '保险时长(/年)', width: 100},
            {
                field: 'opUser', title: '业务员', width: 100, formatter: function (value, row, index) {
                return value ? value.realname : "";
            }
            },
            {
                field: 'auditor', title: '审核人', width: 100, formatter: function (value, row, index) {
                return value ? value.username : "";
            }
            },
            {field: 'totalAmount', title: '投保总金额', width: 100},
            {field: 'state', title: '状态', width: 100, formatter: function (value, row, index) {
                    return value ? "<span style='color: gold'>暂存待提交</span>" : "";
                }},
            {field: 'item', title: '查看产品明细', width: 100}

        ]],
        toolbar: "#tb",//工具
        pagination: true,//分页
        rownumbers: true,//行号
        singleSelect: true//只允许选择一行
    });

    //编辑弹出框
    temOrder_dialog.dialog({
        title: "编辑",
        width: 600,
        height: 550,
        closed: true,
        buttons: "#edit_btns",
        onClose: function () {
            $("#addTemp_form").form("clear");
        }
    })



    //方法抽成对象统一管理
    var methodObj = {
        searchForm: function () {
            var clientName = $("#clientName").textbox('getValue');
            var temSn = $("#temSn").textbox('getValue');
            temOrder_dataGrid.datagrid('load', {
                clientName: clientName,
                temSn:temSn
            });
        },
        //拒保功能
        refuse:function () {
            var row = temOrder_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('提示', '请选中一条数据', 'warning');
                return;
            }
            $.messager.confirm('提示', '您确定要操作吗？', function (r) {
                if (r) {
                    $.get("/temOrder/delete.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('提示', '操作成功', 'info', function () {
                                temOrder_dataGrid.datagrid("reload");
                            })
                        } else {
                            $.messager.alert('提示', data.msg, 'error')
                        }
                    })
                }
            })
        },
        audit:function () {
            var row = temOrder_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('提示', '请选中一条数据', 'warning');
                return;
            }
            $.messager.confirm('提示', '您确定要操作吗？', function (r) {
                if (r) {
                    $.get("/temOrder/audit.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('提示', '操作成功', 'info', function () {
                                temOrder_dataGrid.datagrid("reload");
                            })
                        } else {
                            $.messager.alert('提示', data.msg, 'error')
                        }
                    })
                }
            })
        },

        edit: function () {
            var row = temOrder_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('提示', '请选中编辑数据', 'info');
                return;
            }
            $.get("/client/getClient.do", {id: row.holder.id}, function (data) {
                $("[name='holder.id']").val(data.id);
                $("#clientName").textbox("setValue", data.clientName);
                $("#gender").combobox("setValue", data.gender);
                $("#idCard").textbox("setValue", data.idCard);
                $("#tel").numberbox("setValue", data.tel);
                $("#clientAddress").textbox("setValue", data.clientAddress);

            })

            $.get("/car/getCar.do", {id: row.carNo.id}, function (data) {
                $("#cId").val(data.id);
                $("#cName").textbox("setValue", data.name);
                $("#cBrand").combobox("setValue", data.carbrand);
                $("#cModel").textbox("setValue", data.model);
                $("#cNumber").textbox("setValue", data.number);
                $("#cGas").textbox("setValue", data.gas);
                $("#cDate").datebox("setValue", data.pdate);
                $("#cPrice").numberbox("setValue", data.price);
                $("#cType").combobox("setValue", data.typestate);
                $("#cWeight").combobox("setValue", data.weight);
            })

            $.get("/product/getProduct.do", {id: row.product.id}, function (data) {
                $("#companyId").combobox("setValue", data.company.id);
                $("#productName").textbox("setValue", data.name);
                $("[name='product.id']").val(data.id);
                $("[tag=sn]").text(data.sn);
                $("#annualFee").textbox("setValue", data.annualFee);
            })
            $("[tag=amount]").text()
            $("[name=year]").val(row.year);
            //回显数据
            console.log(row);
            $("#addTemp_form").form("load", row);
            temOrder_dialog.dialog("setTitle", "编辑车辆");
            temOrder_dialog.dialog("open");
        },

        reload: function () {
            temOrder_dataGrid.datagrid("load");
        },

        cancel: function () {
            temOrder_dialog.dialog("close");
        },
        save_edit: function () {
            var row = temOrder_dataGrid.datagrid("getSelected");
            $("#addTemp_form").form("submit", {
                url: "/temOrder/saveOrUpdate.do",
                onSubmit: function (param) {
                    $.get("/client/getClient.do", {id: row.holder.id}, function (data) {
                        $("[name='holder.id']").val(data.id);
                        $("#clientName").textbox("setValue", data.clientName);
                        $("#gender").combobox("setValue", data.gender);
                        $("#idCard").textbox("setValue", data.idCard);
                        $("#tel").numberbox("setValue", data.tel);
                        $("#clientAddress").textbox("setValue", data.clientAddress);
                    })

                    param["holder.clientName"] = $("#clientName").textbox("getValue");
                    param["holder.gender"] = $("#gender").combobox("getValue");
                    param["holder.idCard"] = $("#idCard").textbox("getValue");
                    param["holder.tel"] = $("#tel").numberbox("getValue");
                    param["holder.clientAddress"] = $("#clientAddress").textbox("getValue");

                    $.get("/car/getCar.do", {id: row.carNo.id}, function (data) {
                        $("#cId").val(data.id);
                        $("#cName").textbox("setValue", data.name);
                        $("#cBrand").combobox("setValue", data.carbrand);
                        $("#cModel").textbox("setValue", data.model);
                        $("#cNumber").textbox("setValue", data.number);
                        $("#cGas").textbox("setValue", data.gas);
                        $("#cDate").datebox("setValue", data.pdate);
                        $("#cPrice").numberbox("setValue", data.price);
                        $("#cType").combobox("setValue", data.typestate);
                        $("#cWeight").combobox("setValue", data.weight);
                    })
                    param["carNo.name"] = $("#clientAddress").textbox("getValue");
                    param["carNo.carbrand"] = $("#cBrand").combobox("getValue");
                    param["carNo.model"] = $("#cModel").textbox("getValue");
                    param["carNo.number"] = $("#cNumber").textbox("getValue");
                    param["carNo.gas"] = $("#cGas").textbox("getValue");
                    param["carNo.pdate"] = $("#cDate").datebox("getValue");
                    param["carNo.price"] = $("#cPrice").numberbox("getValue");
                    param["carNo.typestate"] = $("#cType").combobox("getValue");
                    param["carNo.weight"] = $("#cWeight").combobox("getValue");

                    $.get("/product/getProduct.do", {id: row.product.id}, function (data) {
                        $("#companyId").combobox("setValue", data.company.id);
                        $("#productName").textbox("setValue", data.name);
                        $("[name='product.id']").val(data.id);
                        $("[tag=sn]").text(data.sn);
                        $("#annualFee").textbox("setValue", data.annualFee);
                    })
                    param["product.company.id"] = $("#companyId").combobox("getValue");
                    param["product.name"] = $("#productName").textbox("getValue");
                    param["product.sn"] = $("[tag=sn]").text();
                    param["product.annualFee"] = $("#annualFee").textbox("getValue");

                }
            });
            temOrder_dialog.dialog("close");
            temOrder_dataGrid.datagrid("load");
        },
        cancel_edit: function () {
            temOrder_dialog.dialog("close")
        },
        //会员客户
        choose_customer: function () {
            client_dialog.dialog("open");
            client_dialog.dialog("setTitle", "选择客户");
        },

        //投保的车辆
        choose_car: function () {
            car_dialog.dialog("open");
            car_dialog.dialog("setTitle", "选择车辆");
        },

        //添加产品
        search: function () {
            product_dialog.dialog("open");
            product_dialog.dialog("setTitle", "选择保险产品");
            product_dataGrid.datagrid("load")
        },
    };

    //按钮统一绑定事件
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    });
});
//onCharge计算金额
function aggregate() {
    var annualFee = $("#annualFee").textbox("getValue");
    var year = $("[name=year]").val();
    var ret = year * annualFee;
    $("[tag=amount]").text(ret.toFixed(2));
}